/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package lib.model;

import lib.xml.XMLizable;
import java.util.ArrayList;
import java.util.List;

/**
 * The class ItemCategory describes how the factory products must be made, it
 * consists in a ordered task list, that must be all made in the rigth order, to
 * have the item built. It also has a name, a description and a color to
 * differentiate among the others.
 * @see Task
 * @author Jorge Mira Yagüe
 */
public class ItemCategory implements XMLizable {

    private String name;
    private String description;
    private ICColor color;
    private List<Task> tasks;

    /**
     * Builds an empty ItemCategory
     */
    public ItemCategory() {
        this.name = "";
        this.tasks = new ArrayList<Task>();
    }

    /**
     * Builds an item category from the given name
     * @param name the name of the given category to be built
     */
    public ItemCategory(String name) {
        this.name = name;
        this.tasks = new ArrayList<Task>();
    }

    /**
     * Builds an item category from the parameters given
     * @param name the name of the item category
     * @param description the description of the item category
     * @param color the color of the item category
     */
    public ItemCategory(String name, String description, ICColor color) {
        this.name = name;
        this.description = description;
        this.color = color;
        this.tasks = new ArrayList<Task>();
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public ICColor getColor() {
        return color;
    }

    public void setColor(ICColor color) {
        this.color = color;
    }

    public void addTask(Task task) {
        tasks.add(task);
    }

    public Task getTask(int index) {
        return tasks.get(index);
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    public List<Task> getTasks() {
        return tasks;
    }

    /**
     * Swaps the positions from two tasks
     * @param taskA 
     * @param taskB
     */
    public void SwapTasks(int taskA, int taskB) {
        Task tmpTask = tasks.get(taskA);
        tasks.set(taskA, tasks.get(taskB));
        tasks.set(taskB, tmpTask);
    }

    public int getNumTasks() {
        return tasks.size();
    }

    public String toXML() {
        String result = "<ItemCategory ";
        result += "name=\"" + name + "\" ";
        result += "description=\"" + description + "\" ";
        result += "color=\"" + color.toString() + "\" ";
        result += ">\n";
        if (tasks != null) {
            for (Task task : tasks) {
                result += task.toXML();
            }
        }
        result += "</ItemCategory>\n";
        return result;
    }
}
